package cn.itcast.order.config.ijpay.alipay;

import cn.itcast.order.web.ijpay.ijpayconfig.AbsAliPayApiConfig;
import com.alipay.api.AlipayApiException;
import com.ijpay.alipay.AliPayApiConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author sxt
 * @version 1.0
 * @description alipay 支付配置信息
 * @date 2024/4/26 18:28
 */
@SuppressWarnings({"LombokGetterMayBeUsed", "LombokSetterMayBeUsed"})
@Component
@ConfigurationProperties(prefix = "ijpay.alipay")
public class AlipayApiConfigProperties implements InitializingBean, ApplicationContextAware {

    private List<AliPayBean> aliPayConfigs = new ArrayList<>();

    private ApplicationContext applicationContext;


    /**
     * 向缓存注入支付Api配置
     */
    @Override
    public void afterPropertiesSet() {
        if (applicationContext != null) {
            try {
                Map<String, AbsAliPayApiConfig> beansOfType = applicationContext.getBeansOfType(AbsAliPayApiConfig.class);
                if (beansOfType.isEmpty()) {
                    return;
                }

                Collection<AbsAliPayApiConfig> absAliPayApiConfigs = beansOfType.values();
                absAliPayApiConfigs.forEach(aliPayApiConfig -> aliPayConfigs.forEach(configBean -> {
                    try {
                        AliPayApiConfig config = AliPayApiConfig.builder()
                                .setAppId(configBean.getAppId())
                                .setAliPayPublicKey(configBean.getPublicKey())
                                .setAppCertPath(configBean.getAppCertPath())
                                .setAliPayCertPath(configBean.getAliPayCertPath())
                                .setAliPayRootCertPath(configBean.getAliPayRootCertPath())
                                .setCharset("UTF-8")
                                .setPrivateKey(configBean.getPrivateKey())
                                .setServiceUrl(configBean.getServerUrl())
                                .setSignType(configBean.getSignType());
                        if (configBean.isCertType()) {
                            config.buildByCert();
                        } else {
                            config.build();
                        }
                        aliPayApiConfig.setApiConfig(config);
                    } catch (AlipayApiException e) {
                        // do nothing
                    }
                }));
            } catch (Exception e) {
                // do nothing
            }

        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public List<AliPayBean> getAliPayConfigs() {
        return aliPayConfigs;
    }

    public void setAliPayConfigs(List<AliPayBean> aliPayConfigs) {
        this.aliPayConfigs = aliPayConfigs;
    }
}
